

namespace Stee.CAP8.MessageBus.Interface.Models
{
    using System;

    public class TopicConfiguration
    {
        /// <summary>
        /// Topic name
        /// </summary>
        public string Name { get; }

        /// <summary>
        /// Number of topic partitions
        /// </summary>
        public int NumOfPartitions { get; }

        /// <summary>
        /// Number of replicas for each partition
        /// </summary>
        public short ReplicationFactor { get; }


        #region Constructor

        /// <summary>
        /// Initializes a new instance of the TopicConfiguration class
        /// </summary>
        /// <param name="name">Topic name</param>
        /// <param name="numOfPartitions">Number of partitions in a topic. Recommended value is 4 and above.</param>
        /// <param name="replicaFactor">Number of replicas for each partition of a topic. Recommended value is 4 and above</param>
        public TopicConfiguration(string name, int numOfPartitions, short replicaFactor)
        {
            name = name?.Trim();

            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException($"Null or empty message topic encountered: {nameof(name)}");
            }
            else if (numOfPartitions < 1)
            {
                throw new ArgumentNullException($"Topic partition count cannot be less than 1: {numOfPartitions}");
            }
            else if (replicaFactor < 1)
            {
                throw new ArgumentNullException($"Topic replication factor cannot be less than 1: {replicaFactor}");
            }

            this.Name = name;
            this.NumOfPartitions = numOfPartitions;
            this.ReplicationFactor = replicaFactor;
        }

        #endregion Constructor


        /// <summary>
        /// Compare equality method
        /// </summary>
        /// <param name="other">Other TopicName instance</param>
        /// <returns>Return TRUE if other instance is identical else FALSE</returns>
        public bool Equals(TopicName other)
        {
            return this.Equals((object)other);
        }

        /// <summary>
        /// Overridden Equals method
        /// </summary>
        /// <param name="obj">Other TopicName instance</param>
        /// <returns>Return TRUE if other instance is identical else FALSE</returns>
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }
            else
            {
                return (obj is TopicConfiguration) ?
                    string.Equals(this.ToString(), obj.ToString(), StringComparison.InvariantCultureIgnoreCase) : false;
            }
        }

        /// <summary>
        /// Overridden GetHashCode method
        /// </summary>
        /// <returns>Return the hash code</returns>
        public override int GetHashCode()
        {
            return this.Name.GetHashCode(StringComparison.InvariantCultureIgnoreCase);
        }

        /// <summary>
        /// Overridden ToString method
        /// </summary>
        /// <returns>Return the stringified object</returns>
        public override string ToString()
        {
            return $"Topic: {this.Name}, Partition count: {this.NumOfPartitions}, Replication factor: {this.ReplicationFactor}";
        }
    }
}
